<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      首先需要有一个函数，用来计算当前数拆分出的各个值的和。用while(n)存在，
      然后计算各个位的值，最后求和
      */
      var isHappy = function (n) {
        function getSum(n) {
          let sum = 0;
          while (n) {
            sum += (n % 10) ** 2;
            n = parseInt(n / 10);
          }
          return sum;
        }
        let map = new Map();
        let res = n;
        while (true) {
          //用map保存每次的值，如果重复了，说明无限循环
          if (map.has(res)) {
            return false;
          }
          if (res == 1) {
            return true;
          }
          map.set(res, 1);
          res = getSum(res);
        }
      };
    </script>
  </body>
</html>
